package com.shujia.kqzl;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class kqzlMR {
    //编写map类，并重写map方法
    public static class MapKqzl extends Mapper<LongWritable,Text,Text,LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //将hadoop中的文本Text类型,转化成java中的String字符串类型
            String s = value.toString();
            //按照逗号进行字符串分割
            String[] info = s.split(",");

            //过滤出长度大于4，且检测的指标是PM2.5
            //字符串比较值的时候，把要比较的值放在equals前面，避免空指针错误
            if(info.length>4 && "PM2.5".equals(info[2])){
                //遍历每个检测点
                //遍历整个数据的长度作为循环结束条件
                for(int i = 3,j = 1;i<info.length;i++,j++){
                    //处理没有检测到的PM2.5值
                    //""  " "  null
                    if("".equals(info[i]) || " ".equals(info[i]) || info[i]==null){
                        info[i] = "0";
                    }
                    //将日期和监测点的数目作为key,检测到的PM2.5值作为value写出去
                    context.write(new Text(info[0]+","+j),new LongWritable(Long.valueOf(info[i])));
                }
            }
        }
    }

    public static class ReduceKqzl extends Reducer<Text,LongWritable,Text,LongWritable>{
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            //定义变量进行累加
            Long sum = 0L;
            for (LongWritable value : values) {
                //通过get()方法获取值
                long PM_num = value.get();
                //累加求和
                sum += PM_num;
            }

            //将监测点一天24小时PM2.5的总和除以24，得到平均值
            long avg = sum / 24;
            //将结果输出
            context.write(key,new LongWritable(avg));
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //获取Hadoop配置文件
        Configuration conf = new Configuration();
        //使得结果与reduce结果以逗号分割
        //mapred.textoutputformat.separator
        conf.set("mapred.textoutputformat.separator",",");
        //创建Job任务
        Job job = Job.getInstance(conf, kqzlMR.class.getSimpleName());
        //设置运行job的java类
        job.setJarByClass(kqzlMR.class);

        //从HDFS中读取数据
        FileInputFormat.setInputPaths(job,new Path(args[0]));

        //设置map类
        job.setMapperClass(MapKqzl.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        //设置reduce类
        job.setReducerClass(ReduceKqzl.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //设置输出HDFS路径
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //等待启动
        job.waitForCompletion(true);
    }
}
